package com.asb.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.asb.model.PeopleInfo;
import com.asb.model.vo.UserVo;
import com.asb.utils.AccessUtil;
import com.asb.utils.JsonUtil;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Record;
import com.taobao.api.ApiException;

import cn.hutool.core.map.MapUtil;

public class UserDingTalkService {

	public static Logger log = Logger.getLogger(UserDingTalkService.class);

	@Inject
	PeopleService peopleService;

	// 获取所有用户id by mobole
	public String getUserIdByMobile(String mobile) {
		String result = "";
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
			OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
			req.setMobile(mobile);
			OapiV2UserGetbymobileResponse rsp = client.execute(req, AccessUtil.getAccess_token());
			log.info("用户ID: " + rsp.getBody());
			result = rsp.getBody();
		} catch (ApiException e) {
			log.error("",e);
		}

		return result;
	}

	// 获取用户详情
	public String getUserDetail(String userId) {
		String result = "";
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
			OapiV2UserGetRequest req = new OapiV2UserGetRequest();
			req.setUserid(userId);
			OapiV2UserGetResponse rsp = client.execute(req, AccessUtil.getAccess_token());
			log.info("用户详情: " + rsp.getBody());
			result = rsp.getBody();
		} catch (ApiException e) {
			log.error("",e);
		}
		return result;
	}

	// 获取根用户list
	public String getAllUsers(Long DeptId) {
		String result = "";
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
			OapiUserListidRequest req = new OapiUserListidRequest();
			req.setDeptId(DeptId);
			OapiUserListidResponse rsp = client.execute(req, AccessUtil.getAccess_token());
			log.info("用户列表: " + rsp.getBody());
			result = rsp.getBody();
		} catch (ApiException e) {
			log.error("",e);
		}

		return result;
	}

	// 获取所有部门list
	public String getAllDep(Long depId) {
		String result = "";
		try {
			DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
			OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
			req.setDeptId(depId);
			OapiV2DepartmentListsubResponse rsp = client.execute(req, AccessUtil.getAccess_token());
			log.info("部门ID: " + rsp.getBody());
			result = rsp.getBody();
		} catch (ApiException e) {
			log.error("",e);
		}

		return result;
	}

	public List<UserVo> userLoginByUserid(String userid) throws ApiException {
		String detailStr = this.getUserDetail(userid);
		Map<String, Object> map2 = JsonUtil.toRes(detailStr);
		String mobile = String.valueOf(map2.get("mobile"));

		return peopleService.getPeopleInfoByPhone(mobile);
	}

	// code转userid
	public String getUserIdByAuthCode(String authCode) throws ApiException {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
		OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
		req.setCode(authCode);
		OapiV2UserGetuserinfoResponse rsp = client.execute(req, AccessUtil.getAccess_token());
		Map<String, Object> mapLogin = JsonUtil.toRes(rsp.getBody());
		if (mapLogin == null) {
			return null;
		}
		return MapUtil.getStr(mapLogin, "userid");
	}

	// 免密登录
	public List<Map<String, Object>> userLogin(String authCode) throws ApiException {
		DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo");
		OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest();
		req.setCode(authCode);
		OapiV2UserGetuserinfoResponse rsp = client.execute(req, AccessUtil.getAccess_token());
		log.info("免密登录: " + rsp.getBody());
		Map<String, Object> mapLogin = JsonUtil.toRes(rsp.getBody());
		if (mapLogin == null) {
			return null;
		}
		String userid = String.valueOf(mapLogin.get("userid"));
		// String userid = "24583061261136723";
		String detailStr = this.getUserDetail(userid);
		Map<String, Object> map2 = JsonUtil.toRes(detailStr);
		String mobile = String.valueOf(map2.get("mobile"));
		List<String> mobiles = new ArrayList<>();
		mobiles.add(mobile);
		PeopleInfo people = new PeopleInfo();
		people.setMobile(mobiles);
		people.setUserId(userid);

		List<Map<String, Object>> list = peopleService.getPeopleInfoFor(people);

		return list;
	}

}
